
<template>
  <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
    <el-tab-pane label="游戏列表" name="first"/>
    <el-tab-pane label="游戏编辑" name="second">
      <el-form ref="form" label-width="100px">
        <el-form-item label="游戏分组">
          <el-select
            v-model="input.groupName"
            placeholder="请输入关键词"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="游戏名称">
          <el-input v-model="input.name"/>
        </el-form-item>
        <el-form-item label="cp名称">
          <el-select
            v-model="input.company_name"
            placeholder="cp名称"
          >
            <el-option
              v-for="item in options1"
              :key="item.id"
              :label="item.company_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="游戏下载地址">
          <el-input v-model="input.package_url" :disabled="true"/>
        </el-form-item>
        <el-form-item label="游戏版本">
          <el-input v-model="input.version"/>
        </el-form-item>
        <el-form-item label="游戏类型">
          <el-checkbox-group v-model="input.category">
            <el-checkbox v-for="categoryValue in categories" :label="categoryValue.id">{{ categoryValue.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="游戏实名认证">
          <el-radio-group v-model="input.is_auth">
            <el-radio :label="1" border size="small">关闭实名认证</el-radio>
            <el-radio :label="2" border size="small">强制实名认证</el-radio>
            <el-radio :label="3" border size="small">开启实名认证</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="icon图">
          <!--      <el-image-->
          <!--        id="img"-->
          <!--        style="width: 100px; height: 100px"-->
          <!--        :src="url"-->
          <!--        :preview-src-list="srcList"-->
          <!--      @error="upload"-->
          <!--      >-->
          <!--      </el-image>-->
          <!--      <el-button style="display: block;margin-bottom:20px"  size="small" v-show="statusUpload" type="file">更改图片</el-button>-->

          <el-upload
            class="avatar-uploader"
            :action="requestUrl"
            :on-success="handleAvatarSuccess"
            :on-error="handleAvatarError"
          >
            <img v-if="input.icon" :src="input.icon" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即编辑</el-button>
          <el-button @click="back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-tab-pane>
  </el-tabs>
</template>

<style>

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import LzForm from '@c/LzForm'
import LzFormItem from '@c/LzForm/LzFormItem'
import PageContent from '@c/PageContent'
import {
  createGameGames,
  editGameGames,
  storeGameGames,
  updateGameGames,
  getGroups,
  getEditList,
  updateGame,
} from '@/api/game/games'

export default {
  name: 'Form',
  components: {
    LzForm,
    LzFormItem,
    PageContent,
  },
  data() {
    return {
      requestUrl: '/admin-api/iconUpload/' + this.$route.params.id,
      srcList: [],
      activeName: 'second',
      input: {
        name: '',
        package_url: '',
        groupName: '',
        company_name: '',
        version: '',
        category: '',
        icon: '',
        is_auth: '',
        icon_name:'',
      },
      statusUpload: false,
      statusUpload1: true,
      categories: [],
      options: [],
      options1: [],
    }
  },
  mounted() {
    getGroups().then((response) => {
      this.options = response.data.res
      this.options1 = response.data.to
    }),

    getEditList(this.$route.params.id).then((response) => {
      this.input = response.data.data.[0]
      console.log(this.input)
      this.categories = response.data.res
      this.srcList = [response.data.data.[0].icon]
    })
  },
  methods: {
    async getData($form) {
      let data

      if ($form.realEditMode) {
        ({ data } = await editGameGames($form.resourceId))
      } else {
        ({ data } = await createGameGames())
      }

      return data
    },
    // beforeAvatarUpload(file) {
    //   const isJPG = file.type === 'image/jpeg';
    //   const isLt2M = file.size / 1024 / 1024 < 2;
    //
    //   // if (!isJPG) {
    //   //   this.$message.error('上传头像图片只能是 JPG 格式!');
    //   // }
    //   if (!isLt2M) {
    //     this.$message.error('上传头像图片大小不能超过 2MB!');
    //   }
    //   return isJPG && isLt2M;
    // },
    handleAvatarSuccess(res, file) {
      this.input.icon_name = res
      this.input.icon = URL.createObjectURL(file.raw)
      this.$message.success('上传成功')
    },
    handleAvatarError() {
      this.$message.error('上传失败')
    },
    async onSubmit() {
      await updateGame(this.$route.params.id, this.input).then((response)=>{
         if(response.status==200){
           this.$message.success(response.data)
           this.$router.replace('/game/games')

         }else{
           this.$message.error(response.data);
         }
      }).catch((error)=>{
        this.$message.error('请求失败')

      })
    },
    back(){
      this.$router.replace('/game/games')
    },

    handleClick(tab) {
      if (tab.index == 0) {
        this.$router.replace('/game/games')
      }
    },
  },

}
</script>
